// packageJ/priceDifferencePool/index.js
const app = getApp();
const skinBehavior = require('../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    show: false,
    info: [],
    pool_log: [],
    popupItem: [],
    diyname: "资金池",

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
    let basic_info = wx.getStorageSync("yz_basic_info");
    wx.setNavigationBarTitle({
      title: basic_info.price_difference_pool_name?basic_info.price_difference_pool_name:'资金池',
    })
    this.setData({
      diyname:basic_info.price_difference_pool_name?basic_info.price_difference_pool_name:'资金池'
    })
  },
  popupShow(e) {
    this.setData({
      show: true,
      popupItem: e.currentTarget.dataset.item
    })
  },
  closePopup(e) {
    this.setData({
      show: false,
    })
  },
  closePopupleave() {
    this.setData({
      popupItem: []
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toBalance_recharge() {
    wx.navigateTo({
      url: '/packageA/member/balance/balance_recharge/balance_recharge?name=pdp',
    })
  },
  tobalance_withdrawalspdp() {
    wx.navigateTo({
      url: '/packageA/member/balance/balance_withdrawals/balance_withdrawals?name=pdp',
    })
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.price-difference-pool.frontend.controllers.page.index");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.pool_log.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            info: res.data,
            pool_log: res.data.pool_log.data,
          })
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      urlStr = app.getNetAddresss('plugin.price-difference-pool.frontend.controllers.page.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              pool_log: this.data.pool_log.concat(res.data.pool_log.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})